var express = require('express');
var router = express.Router();
var { usermodel, rolemodel, menumodel ,xiaoqumodel,wuzimodel,carmodel,renmodel,renwumodel} = require('../model/model')
var jwt = require('jsonwebtoken')
var multiparty = require('multiparty')
//用户表
//用户添加
router.post('/adduser', (req, res) => {
  usermodel.create(req.body)
  res.send({
    code: 200,
    msg: '添加用户成功'
  })
})

//用户修改
router.post('/uppuser',async(req,res)=>{
  console.log(req.body);
  await usermodel.updateOne({_id:req.body._id},req.body)
  res.send({
    code: 200,
    msg:'修改成功',
  })
})

//角色添加
router.post('/addrole', (req, res) => {
  console.log(req.body);
  rolemodel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功'
  })
})

//角色删除
router.delete('/del/:_id', async (req,res)=>{
  let {_id} = req.params
  await usermodel.deleteOne({_id:_id})
  res.send({
    code:201,
    msg:'删除成功'
  })
})

//权限添加
router.post('/addmenu', (req, res) => {
  console.log(req.body);
  menumodel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功'
  })
})

//无限极
router.get('/quanlist', async (req, res) => {
  let {roleid} = req.query
  let  [{menuid}]  = await rolemodel.find({ _id:roleid })
  let newlist = []
  let qq = await menumodel.find().lean()
  menuid.forEach((_id) => {
    qq.forEach(item => {
      if (String(_id) === String(item._id)) {
        newlist.push(item)
      }
    })
  })
  let qxobj = {}
  qq.forEach((item) => {
    qxobj[item._id] = item
  })
  let data = []
  newlist.forEach((i) => {
    if (!i['pid']) {
      data.push(i)
    } else {
      if (qxobj[i['pid']] && !qxobj[i['pid']]['children']) {
        qxobj[i['pid']]['children'] = []
      }
      if (qxobj[i['pid']]) qxobj[i['pid']]['children'].push(i)
    }
  })
  res.send({
    data
  })
})

//用户列表
router.get('/userlist', async (req, res) => {
  let { zhanghao, name, roleid } = req.query
  let query = []
  if (zhanghao) {query.push({ zhanghao: zhanghao })}
  if (name) {query.push({ name: name })}
  if (roleid) {query.push({ roleid: roleid })}
  let list 
  let total
  let wanggelist = await usermodel.find({name:/网格/})
  if(query.length!=0){
    list = await usermodel.find({$and:query})
    total = await usermodel.countDocuments({$and:query})
  }else{
    list = await usermodel.find()
    total = await usermodel.countDocuments()
  }
  res.send({
    list,
    total,
    wanggelist
  })
})

//角色列表
router.get('/rolelist', async (req, res) => {
  let rolelist = await rolemodel.find()
  res.send({
    rolelist
  })
})

//登录
router.post('/login', async (req, res) => {
  let list = await usermodel.findOne({ zhanghao: req.body.zhanghao })
  console.log(list)
  if (list) {
    if (list.password == req.body.password) {
      let token = "Bearer " + jwt.sign({ username: 'fang' }, "xiaozz", { expiresIn: 60 * 60 * 24 })
      res.send({
        code: 200,
        msg: '登录成功',
        list,
        token
      })
    } else {
      res.send({
        code: 202,
        msg: '密码错误'
      })
    }
  } else {
    res.send({
      code: 400,
      msg: '用户名不存在'
    })
  }
})

//上传图片
router.post('/upload', (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir = "upload"
  form.parse(req, (err, file, imgData) => {
    console.log(imgData);
    res.send({
      code: 200,
      msg: '获取到图片',
      path: "http://localhost:3000/" + imgData.file[0].path
    })
  })
})

//小区
router.post('/addxiaoqu', (req, res) => {
  xiaoqumodel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功'
  })
})

//小区列表
router.get('/xiaoqulist', async (req, res) => {
  let list = await xiaoqumodel.find()
  let list1 = await xiaoqumodel.find({name:'富力江湾'})
  res.send({
    list,
    list1
  })
})

//房屋添加
router.post('/addwuzi',(req,res)=>{
  wuzimodel.create(req.body)
  res.send({
    code: 200,
    msg: '添加房屋成功'
  })
})

//房屋查询
router.get('/wuzilist', async (req, res) => {
  let { xiaoqu, huzhu } = req.query
  let query = []
  if (xiaoqu) {query.push({ xiaoqu: xiaoqu })}
  if (huzhu) {query.push({ huzhu: huzhu })}
  let wuzilist 
  let total
  if(query.length!=0){
    wuzilist = await wuzimodel.find({$and:query})
    total = await wuzimodel.countDocuments({$and:query})
  }else{
    wuzilist = await wuzimodel.find()
    total = await wuzimodel.countDocuments()
  }
  res.send({
    wuzilist,
    total
  })
})

//车辆添加
router.post('/addcar',(req,res)=>{
  carmodel.create(req.body)
  res.send({
    code: 200,
    msg: '添加车辆成功'
  })
})

//车辆查询
router.get('/carlist', async (req, res) => {
  let { xiaoqu, ren, carid } = req.query
  let query = []
  if (xiaoqu) {query.push({ xiaoqu: xiaoqu })}
  if (carid) {query.push({ carid: carid })}
  if (ren) {query.push({ ren: ren })}
  let carlist 
  let total
  let wangge1list = await carmodel.find({xiaoqu:'富力江湾'})
  if(query.length!=0){
    carlist = await carmodel.find({$and:query})
    total = await carmodel.countDocuments({$and:query})
  }else{
    carlist = await carmodel.find()
    total = await carmodel.countDocuments()
  }
  res.send({
    carlist,
    total,
    wangge1list
  })
})

//住户添加
router.post('/addren',(req,res)=>{
  renmodel.create(req.body)
  res.send({
    code:200,
    msg:'添加住户成功'
  })
})

//住户查询
router.get('/renlist', async (req, res) => {
  let { xiaoqu, sex,name } = req.query
  let query = []
  if (xiaoqu) {query.push({ xiaoqu: xiaoqu })}
  if (name) {query.push({ name: name })}
  if (sex) {query.push({ sex: sex })}
  let renlist 
  let total
  let renlist1 = await renmodel.find({xiaoqu:'富力江湾'})
  if(query.length!=0){
    renlist = await renmodel.find({$and:query})
    total = await renmodel.countDocuments({$and:query})
  }else{
    renlist = await renmodel.find()
    total = await renmodel.countDocuments()
  }
  res.send({
    renlist,
    total,
    renlist1
  })
})

//数量
router.get('/count', async (req, res) => {
  let c1 = await xiaoqumodel.find().count()
  let c2 = await wuzimodel.find().count()
  let c3 = await renmodel.find().count()
  let c4 = await carmodel.find().count() 


  let cc1 = await xiaoqumodel.find({name:'富力江湾'}).count()
  let cc2 = await wuzimodel.find({xiaoqu:'富力江湾'}).count()
  let cc3 = await renmodel.find({xiaoqu:'富力江湾'}).count()
  let cc4 = await carmodel.find({xiaoqu:'富力江湾'}).count() 
  // console.log(c1, c2, c3, c4);
  // let arr = [{'xiao':c1},{'wu':c2},{'prople':c3},{'car':c4}]
  let arr = [{value:c1,name:'小区总数'},{value:c2,name:'房屋总数'},{value:c3,name:'人员总数'},{value:c4,name:'车辆总数'}]
  let arr1 = [{value:cc1,name:'小区总数'},{value:cc2,name:'房屋总数'},{value:cc3,name:'人员总数'},{value:cc4,name:'车辆总数'}]
  console.log(arr);
  res.send({
    msg:'ok',
    arr,
    arr1
  })
})


//任务
router.post('/addrenwu',(req,res)=>{
  renwumodel.create(req.body)
  res.send({
    code:200,
    msg:'添加任务成功'
  })
})

//任务列表
router.get('/renwulist',async (req,res)=>{
  let list = await renwumodel.find()
  let weilist  = await renwumodel.find({zhiliang:'待评估'})
  res.send({
    list,
    weilist
  })
})


//撤回任务
router.delete('/chehui/:_id', async (req,res)=>{
  let {_id} = req.params
  await renwumodel.deleteOne({_id:_id})
  res.send({
    code:201,
    msg:'删除成功'
  })
})

//任务接收
router.post('/jieshou',async(req,res)=>{
  console.log(req.body);
  await renwumodel.updateOne({_id:req.body._id},req.body)
  res.send({
    code: 200,
    msg:'接收成功',
  })
})


// 获取yzm图片地址
// router.get('/yzmimgs', async (req, res) => {
//   let yzm = await yzmmodel.find()
//   res.send({
//     yzm
//   })
// })


module.exports = router;
